package com.applink.sell_proof.builder;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.applink.sell_proof.BasePagerActivity;
import com.applink.sell_proof.R;
import com.applink.sell_proof.TipNotificationDialog;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.IntentKey;
import com.applink.sell_proof.model.Tip;

public class TipBuilder extends AbstractListBuilder implements
		OnItemClickListener {

	public TipBuilder(Context context, ViewGroup parent) {
		super(context, parent);
		((ListView) parent.findViewById(android.R.id.list))
				.setOnItemClickListener(this);
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		int amount_header = ((ListView) parent).getHeaderViewsCount();
		Tip tip = (Tip) mData.get(position - amount_header);
		Intent intent = new Intent(mContext, TipNotificationDialog.class);
		int indexActivity = ((BasePagerActivity) mContext).getActivityIndex();
		intent.putExtra(IntentKey.ACTIVITY_NAME, indexActivity);
		intent.putExtra(Constant.KEY_TIP, tip);
		mContext.startActivity(intent);
	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.tip_item, null);
		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.tip_name, base.findViewById(R.id.tip_name));
		views.put(R.id.tip_paragraph, base.findViewById(R.id.tip_paragraph));
		views.put(R.id.tip_save_icon, base.findViewById(R.id.tip_save_icon));
		views.put(R.id.tip_date, base.findViewById(R.id.tip_date));

		return views;
	}

	@Override
	public void builConvertViewContentData(int position,
			ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Tip tip = (Tip) mData.get(position);
		((TextView) views.get(R.id.tip_name)).setText(tip.getName());

		((TextView) views.get(R.id.tip_paragraph)).setText(tip.getParagraph());
		//views.get(R.id.tip_save_icon).setVisibility(View.VISIBLE);
		views.get(R.id.tip_save_icon).setVisibility(
				tip.isSave() ? View.VISIBLE : View.INVISIBLE);
		String date = DateFormat.getDateFormat(mContext).format(
				
				
				new Date(tip.getDate()));
		((TextView) views.get(R.id.tip_date)).setText(date);

	}

}
